package com.jr.dao.impl;

import com.jr.dao.IProjectInfoDao;
import com.jr.pojo.ProjectInfo;
import com.jr.util.DBHelper;
import com.jr.util.PageHelper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class ProjectInfoDaoImpl implements IProjectInfoDao {

    private DBHelper db=new DBHelper();

    @Override
    public ArrayList<ProjectInfo> selectByPid(Integer pid) {

        String sql="select * from projectinfo where pid=?";
        return db.dtQuery(sql,ProjectInfo.class,pid);

    }

    @Override
    public int updateProject(ProjectInfo pro) {

        String sql="update projectinfo set status=? where pid=?";
        Object[] objs={pro.getStatus(),pro.getPid()};
        return db.dtUpdate(sql,objs);
    }

    @Override
    public int selectDataCount(Integer status) {
        StringBuilder sql=new StringBuilder("select count(pid) from projectinfo where 1=1");
        if(status>=0 && status<=2){
            sql.append(" and status="+status);
        }
        Connection con=null;
        PreparedStatement ps=null;
        ResultSet resultSet=null;
        try {
             con = db.getCon();
             ps = con.prepareStatement(sql.toString());
             resultSet = ps.executeQuery();
            if (resultSet.next()) {
                return resultSet.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            db.closeAll(con,ps,resultSet);
        }
        return 0;
    }

    @Override
    public ArrayList<ProjectInfo> selectByStatus(Integer status, PageHelper ph) {
        StringBuilder str=new StringBuilder("select * from projectinfo where 1=1 ");
        if(status>=0 && status<=2){
            str.append(" and status="+status);
        }
        str.append(" limit ?,?");
        int index=(ph.getPageIndex()-1)*ph.getShowNumber();
        return db.dtQuery(str.toString(),ProjectInfo.class,index,ph.getShowNumber());
    }

}
